package cn.rdq.iknessplatform.component;


import cn.rdq.common.CommonResult;
import cn.rdq.common.exception.BaseException;
import cn.rdq.common.exception.BusinessException;
import cn.rdq.common.exception.ExceptionStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestClientException;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常捕获
 */
@ControllerAdvice
class GlobalExceptionHandler {

    protected static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public CommonResult handleValidException(MethodArgumentNotValidException exception) throws Exception {
        log.error("方法参数数据出现异常,异常数据为:{},错误属性为{},错误消息为{}",
                exception.getBindingResult().getFieldError().getRejectedValue(), exception.getBindingResult().getFieldError().getField(), exception.getBindingResult().getFieldError().getDefaultMessage());
        CommonResult commonResult = new CommonResult();
        commonResult.failed(exception.getBindingResult().getFieldError().getRejectedValue() + "," + exception.getBindingResult().getFieldError().getDefaultMessage(), ExceptionStatus.BadRequest);
        return commonResult;
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.toString());
        log.error("未知异常", e);
        return commonResult;
    }

    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public CommonResult baseExceptionHandler(BaseException e) {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.getMessage());
        log.error("非业务异常", e);
        return commonResult;
    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public CommonResult baseExceptionHandler(BusinessException e) {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.getMessage());
        log.error("业务异常", e);
        return commonResult;
    }

    @ExceptionHandler(value = IllegalArgumentException.class)
    @ResponseBody
    public CommonResult illegalArgumentExceptionHandler(IllegalArgumentException e) {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.getMessage());
        log.error("参数异常", e);
        return commonResult;
    }


    @ExceptionHandler(IllegalStateException.class)
    @ResponseBody
    private CommonResult handelIllegalStateException(IllegalStateException e) {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.getMessage());
        log.error("断言状态异常", e);
        return commonResult;
    }

    @ExceptionHandler(RestClientException.class)
    @ResponseBody
    private CommonResult handelRestClientException(RestClientException e) {
        CommonResult commonResult = new CommonResult();
        commonResult.failed(e.getMessage());
        log.error("restTemplate远程调用发生错误", e);
        return commonResult;
    }


}
